package com.isoftstone.disk.obs.controller;

import com.alibaba.fastjson.JSON;
import com.isoftstone.disk.mybatis.entity.PtFileEntity;
import com.isoftstone.disk.mybatis.flexigrid.PadingRstType;
import com.isoftstone.disk.mybatis.flexigrid.PagingBean;
import com.isoftstone.disk.mybatis.ztree.ZtreeEntity;
import com.isoftstone.disk.obs.entity.SearchObsEntity;
import com.isoftstone.disk.obs.service.ObsService;
import com.isoftstone.disk.spring.controller.BaseController;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/file")
public class ObsController extends BaseController {
    private Logger logger = LogManager.getLogger(this.getClass().getName());
    @Resource
    private ObsService obsService;
    @Secured("ROLE_cldk_file_All")
    @RequestMapping("/fileAll")
    public String fileAll(){
        return "file/fileAll";
    }
    @Secured("ROLE_cldk_file_All")

    @RequestMapping("/refreshObsFile")
    @ResponseBody
    public String refreshObsFile(){

        obsService.refreshObsFile();
        return  jsonSuccess("file.refresh.obs.success");
    }
    @RequestMapping("/getFileListByPage")
    @ResponseBody
    public PadingRstType<PtFileEntity> getFileListByPage(SearchObsEntity entity, PagingBean paging){

        PadingRstType<PtFileEntity>  padingRstType = obsService.getFileListByPage(entity,paging);
        return  padingRstType;
    }

    @RequestMapping("/upload")
    @ResponseBody
    public String uploadFile(MultipartHttpServletRequest request) throws IOException {
        MultipartFile file = request.getFile("fileName");
        //file.getInputStream();
        //file.getOriginalFilename();
        obsService.uploadFile(file.getOriginalFilename(),file.getInputStream());
        return jsonSuccess("file.upload.success");
    }
    @RequestMapping("/download/{fileUUid}")
    @ResponseBody
    public ResponseEntity<byte[]> download(@PathVariable("fileUUid") String fileUUId) throws IOException {
        logger.info("fileUUid:" + fileUUId);

        ResponseEntity<byte[]> responseEntity = obsService.download(fileUUId);
        return responseEntity;
    }

    @RequestMapping("/moveFile")
    @ResponseBody
    public String moveFile(@RequestParam("fileUuid") String fileUuid,@RequestParam("dirUuid")  String dirUuid) throws IOException {

        obsService.moveFile(fileUuid,dirUuid);
        return  jsonSuccess("file.refresh.obs.success");

    }


    @RequestMapping("/renameFile")
    @ResponseBody
    public String renameFile(@RequestParam("fileUuid") String fileUuid,@RequestParam("fileName")  String fileName) throws IOException {

        obsService.renameFile(fileUuid,fileName);
        return  jsonSuccess("file.refresh.obs.success");

    }


    @RequestMapping("/queryDirZtreeListByFileId")
    @ResponseBody
    public List<ZtreeEntity> queryDirZtreeListByFileId()  {

        List<ZtreeEntity> list =  obsService.queryDirZtreeListByFileId();
        return  list;

    }
    //预览视频
    @RequestMapping("/getShareUrl")
    @ResponseBody
    public String getFileUUid(@RequestParam("fileUuid") String fileUUid) {
        String url = obsService.getFileUUid(fileUUid);
        Map<String,Object> map = new HashMap<>();
        map.put("success",true);
        map.put("msg",url);
        map.put("description",url);
        return JSON.toJSONString(map);
    }

    //删除文件到回收站


    @RequestMapping("/huishoufile")
    public String huishoufile(){
        return "file/huishou";
    }

    @RequestMapping("/delfile")
    @ResponseBody
    public String delfile(@RequestParam("fileUuid") String fileUUId) {
        logger.info("fileUUid:" + fileUUId);
//
//        PtFileEntity file = obsService.getFile(fileUUId) ;
        obsService.updatefile(fileUUId);
        return jsonSuccess("file.del");
    }
    //回收站展示
    @RequestMapping("/getFileList")
    @ResponseBody
    public PadingRstType<PtFileEntity> getFileList(SearchObsEntity entity, PagingBean paging){

        PadingRstType<PtFileEntity>  padingRstType = obsService.getFileList(entity,paging);
        return  padingRstType;
    }

    //彻底删除
    @RequestMapping("/del")
    @ResponseBody
    public String del(@RequestParam("fileUuid") String fileUUId) {
        logger.info("fileUUid:" + fileUUId);

        obsService.del(fileUUId);
        return jsonSuccess("file.del");
    }


}
